package cn.tsoft.taobao.ui;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import cn.tsoft.taobao.db.TaoBaoDataManager;
import cn.tsoft.taobao.logic.ITaoBaoActivity;
import cn.tsoft.taobao.logic.TaskManagerActivity;
import cn.tsoft.vo.ItemForDetail;

public class RecommendItemDetailInfoActivity extends TaskManagerActivity
		implements  ITaoBaoActivity {
	
	public static final int LOADE_PRODUCT = 1;
	

	private TextView recommenditem_detail_title;
	private TextView recommenditem_detail_price;
	private TextView recommenditem_detail_num;
	private TextView recommenditem_detail_freight_payer;
	private TextView recommenditem_detail_desc;
	   
	private LinearLayout linearLayout;
	
	GridView bottomMenu;
	
	private ItemForDetail itemRecommend =null;
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.recommenditem_detail);
		// 底部菜单栏点击事件
		setBottomMenu();
		// 映射控件
		this.recommenditem_detail_title = (TextView)findViewById(R.id.recommenditem_detail_title);
		this.recommenditem_detail_price = (TextView)findViewById(R.id.recommenditem_detail_price);
		this.recommenditem_detail_num = (TextView)findViewById(R.id.recommenditem_detail_num);
		this.recommenditem_detail_freight_payer = (TextView)findViewById(R.id.recommenditem_detail_freight_payer);
		this.recommenditem_detail_desc = (TextView)findViewById(R.id.recommenditem_detail_description);
		
		this.linearLayout = (LinearLayout) findViewById(R.id.recommenditem_detail_piclayout);
		linearLayout.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				linearLayout.setBackgroundColor(Color.YELLOW);
				linearLayout.setBackgroundColor(Color.TRANSPARENT);
				// 进入商品图片Activity
				Intent intent = new Intent();
				intent.putExtra("pic_url", itemRecommend.getPic_url());
				intent.setClass(RecommendItemDetailInfoActivity.this,
						ItemDetailInfoPic.class);
				startActivity(intent);
			}
		});
	  	  //添加到Activity组件集合
		getStuffApplication().allActivity.add(this);
	}

	private void setBottomMenu() {
		bottomMenu = (GridView) this.findViewById(R.id.bottomMenu);
		bottomMenu.setBackgroundResource(R.drawable.channelgallery_bg);
		bottomMenu.setNumColumns(5);
		bottomMenu.setGravity(Gravity.CENTER);
		bottomMenu.setVerticalSpacing(10);
		bottomMenu.setHorizontalSpacing(10);
		
		List<Map<String, Object>> data = new ArrayList<Map<String,Object>>();
		Map<String, Object>  map = new HashMap<String, Object>();
		map.put("itemImage", R.drawable.menu_delete);
		map.put("itemText","返回");
		data.add(map);
		
		Map<String, Object>  map1 = new HashMap<String, Object>();
		map1.put("itemImage", R.drawable.menu_delete);
		map1.put("itemText","修改");
		data.add(map1);
		
		Map<String, Object>  map2 = new HashMap<String, Object>();
		map2.put("itemImage", R.drawable.menu_delete);
		map2.put("itemText","下架");
		data.add(map2);
		
		Map<String, Object>  map3 = new HashMap<String, Object>();
		map3.put("itemImage", R.drawable.menu_delete);
		map3.put("itemText","主页");
		data.add(map3);
		
		SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.item_menu,new String[]{"itemImage","itemText"},new int[]{R.id.item_image,R.id.item_text});
		bottomMenu.setAdapter(adapter);
		
		bottomMenu.setOnItemClickListener(new OnItemClickListener(){
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent intent  ;
				switch(position) {
					case 0 :
						intent = new Intent(RecommendItemDetailInfoActivity.this,ProductItem.class);
//						startActivityForResult(intent, 0);
						startActivity(intent);
						break;
					case 1: 
						break;
					case 2: 
						TaoBaoDataManager.proudctDelisting(itemRecommend.getNum_iid());
						break;
					case 3: 
						intent = new Intent(RecommendItemDetailInfoActivity.this,HomeActivity.class);
						startActivity(intent);
						break;
				}
			}});
		
	}



	@Override
	public void init() {
		
	}

	@Override
	public void refresh(Object... param) {
		switch(((Integer)param[0]).intValue())
		{
		case -100://
			Toast.makeText(this,"获取失败", 1000).show();
			break;
		case LOADE_PRODUCT:
			itemRecommend  = (ItemForDetail) param[1];
			recommenditem_detail_title.setText(itemRecommend.getTitle());
			recommenditem_detail_price.setText(recommenditem_detail_price.getText()+"\t"+itemRecommend.getPrice());
			recommenditem_detail_num.setText(recommenditem_detail_num.getText()+"\t"+itemRecommend.getNum());
			recommenditem_detail_freight_payer.setText(recommenditem_detail_freight_payer.getText()+"\t"+itemRecommend.getFreight_payer());
			recommenditem_detail_desc.setText(recommenditem_detail_desc.getText()+"\n"+itemRecommend.getDesc());
	       getStuffApplication().allActivity.remove(this);
		}
	}

}
